
import gdb
import logging


def get_int(varname:str):
    val = gdb.execute(f"p/x {varname}", to_string=True)
    val = val.strip().split()[-1]
    val = int(val, 16)
    return val

class XPrint(gdb.Command):
    def __init__(self):
        super(XPrint, self).__init__('xpri', gdb.COMMAND_USER)

    def invoke(self, args, from_tty):
        argv = gdb.string_to_argv(args)
        for arg in argv:
            print(get_int(arg))

XPrint()

class WState(gdb.Command):
    def __init__(self):
        super(WState, self).__init__('wstate', gdb.COMMAND_USER)

    def invoke(self, args, from_tty):
        while True:
            state = get_int('system_state')
            print(f"system_state: {state}")
            gdb.execute('tbreak __schedule', to_string=True)
            if state >= 1:
                break
            tmp = gdb.execute('bt', to_string=True)
            print(tmp, end='')
            gdb.execute('c', to_string=True)

        gdb.execute('info b', to_string=True)

WState()

